from sqlalchemy import Column, BigInteger, String, SmallInteger, Integer, DateTime, Numeric, Index
from app.config.database import Base
from datetime import datetime
from pydantic import BaseModel, ConfigDict
from typing import Optional
from decimal import Decimal

class Course(Base):
    __tablename__ = "edu_course"
    
    id = Column(BigInteger, primary_key=True, autoincrement=True, comment="课程ID")
    course_code = Column(String(20), nullable=False, unique=True, comment="课程编码")
    course_name = Column(String(100), nullable=False, comment="课程名称")
    course_type = Column(SmallInteger, nullable=False, comment="课程类型：1-主科 2-副科 3-活动课")
    grade_range = Column(String(20), comment="适用年级范围（如：1-2,3-5）")
    credit = Column(Numeric(3,1), comment="学分")
    course_hours = Column(Integer, comment="课时数")
    course_desc = Column(String(500), comment="课程描述")
    status = Column(SmallInteger, nullable=False, default=1, comment="状态：0-停用 1-启用")
    create_time = Column(DateTime, default=datetime.utcnow, nullable=False, comment="创建时间")
    update_time = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False, comment="更新时间")
    
    # 索引
    __table_args__ = (
        Index('uk_course_code', 'course_code'),
    )

class CourseInDB(BaseModel):
    id: int
    course_code: str
    course_name: str
    course_type: int
    grade_range: Optional[str] = None
    credit: Optional[Decimal] = None
    course_hours: Optional[int] = None
    course_desc: Optional[str] = None
    status: int = 1
    create_time: Optional[datetime] = None
    update_time: Optional[datetime] = None

    model_config = ConfigDict(from_attributes=True)